package com.vortex.cloud.zhsw.xinyang.util;

import com.vortex.cloud.zhsw.xinyang.dto.response.GpsDTO;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

/**
 * @author mumuwen
 * @date 2020-11-11 9:40 上午
 */
public class LonLatUtil {


    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid) {

        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);

        return geoCurve.getEllipsoidalDistance();
    }

    /**
     * 根据正北角度计算经纬度
     */
    public static GpsDTO getGpsByAngle(double firstLon, double firstLat, double secondLon, double secondLat, double meter) {
        //角度
        MyLatLng aa = new MyLatLng(firstLon, firstLat);
        MyLatLng bb = new MyLatLng(secondLon, secondLat);
        double brang = getAngle(aa, bb);

        LonLatTest3 test = new LonLatTest3();
        GpsDTO dto = new GpsDTO();
        if (meter == 0) {
            dto.setLon(firstLon);
            dto.setLat(firstLat);
        } else {
            dto = test.computerThatLonLat(firstLon, firstLat, brang, meter);
        }
        return dto;
    }

    /**
     * 获取AB连线与正北方向的角度
     *
     * @param a A点的经纬度
     * @param b B点的经纬度
     * @return AB连线与正北方向的角度（0~360）
     */
    public static double getAngle(MyLatLng a, MyLatLng b) {
        double dx = (b.mRadLo - a.mRadLo) * a.ed;
        double dy = (b.mRadLa - a.mRadLa) * a.ec;
        double angle = 0.0;
        angle = Math.atan(Math.abs(dx / dy)) * 180. / Math.PI;
        double dLo = b.mLongitude - a.mLongitude;
        double dLa = b.mLatitude - a.mLatitude;
        if (dLo > 0 && dLa <= 0) {
            angle = (90. - angle) + 90;
        } else if (dLo <= 0 && dLa < 0) {
            angle = angle + 180.;
        } else if (dLo < 0 && dLa >= 0) {
            angle = (90. - angle) + 270;
        }
        return angle;
    }

}
